/*
 * @Author: ecitlm 
 * @Date: 2017-06-29 16:29:26 
 * @Last Modified by: ecitlm
 * @Last Modified time: 2017-06-29 17:11:37
 */

"use strict";

const express = require('express')
const http = require('http')
const cheerio = require("cheerio")
const app = express()
const request = require("request")
const Iconv = require('iconv-lite')
const sleep = require('sleep')

var portIndex = 0;
var portArr = [{"idd":14,"id":7670,"sheng":"江苏","gangkou":"镇江","Longitude":119.42523,"Latitude":32.064519,"paixu":13,"count":204},{"idd":19,"id":5404,"sheng":"江苏","gangkou":"南通","Longitude":120.888897,"Latitude":31.994101,"paixu":5,"count":107},{"idd":60,"id":4677,"sheng":"江苏","gangkou":"苏州","Longitude":120.583191,"Latitude":31.3124108,"paixu":3,"count":101},{"idd":59,"id":4360,"sheng":"江苏","gangkou":"无锡","Longitude":120.318581,"Latitude":31.50762,"paixu":2,"count":92},{"idd":21,"id":4174,"sheng":"江苏","gangkou":"泰州","Longitude":119.923039,"Latitude":32.479719,"paixu":6,"count":73},{"idd":30,"id":3951,"sheng":"江西","gangkou":"九江","Longitude":116.009302,"Latitude":29.724536,"paixu":32,"count":172},{"idd":27,"id":3491,"sheng":"安徽","gangkou":"铜陵","Longitude":117.797986,"Latitude":30.965148,"paixu":29,"count":255},{"idd":13,"id":3449,"sheng":"上海","gangkou":"上海","Longitude":121.485796,"Latitude":31.224546,"paixu":1,"count":53},{"idd":26,"id":3273,"sheng":"安徽","gangkou":"芜湖","Longitude":118.4409493,"Latitude":31.366136,"paixu":28,"count":166},{"idd":24,"id":2614,"sheng":"江苏","gangkou":"南京","Longitude":118.801092,"Latitude":32.064519,"paixu":14,"count":83},{"idd":68,"id":2597,"sheng":"山东","gangkou":"济宁","Longitude":116.579618,"Latitude":35.437025,"paixu":19,"count":27},{"idd":47,"id":2589,"sheng":"湖北","gangkou":"武汉","Longitude":114.307552,"Latitude":30.611771,"paixu":36,"count":42},{"idd":42,"id":2409,"sheng":"湖北","gangkou":"黄冈","Longitude":114.864807,"Latitude":30.476412,"paixu":35,"count":160},{"idd":25,"id":2134,"sheng":"安徽","gangkou":"马鞍山","Longitude":118.490295,"Latitude":31.711325,"paixu":20,"count":87},{"idd":66,"id":2128,"sheng":"江苏","gangkou":"徐州","Longitude":117.281944,"Latitude":34.224801,"paixu":11,"count":37},{"idd":18,"id":2120,"sheng":"江苏","gangkou":"常州","Longitude":119.941437,"Latitude":31.838043,"paixu":4,"count":43},{"idd":73,"id":1434,"sheng":"江西","gangkou":"南昌","Longitude":115.825329,"Latitude":28.707723,"paixu":33,"count":48},{"idd":64,"id":1367,"sheng":"江苏","gangkou":"淮安","Longitude":119.020489,"Latitude":33.634358,"paixu":10,"count":36},{"idd":31,"id":1303,"sheng":"湖北","gangkou":"黄石","Longitude":115.034245,"Latitude":30.23708,"paixu":34,"count":73},{"idd":52,"id":957,"sheng":"湖北","gangkou":"宜昌","Longitude":111.281197,"Latitude":30.715152,"paixu":39,"count":111},{"idd":17,"id":909,"sheng":"安徽","gangkou":"池州","Longitude":117.493033,"Latitude":30.672914,"paixu":30,"count":109},{"idd":54,"id":867,"sheng":"重庆","gangkou":"重庆","Longitude":106.557691,"Latitude":29.600004,"paixu":47,"count":99},{"idd":22,"id":801,"sheng":"江苏","gangkou":"扬州","Longitude":119.406832,"Latitude":32.409957,"paixu":9,"count":37},{"idd":70,"id":799,"sheng":"安徽","gangkou":"淮南","Longitude":117.002756,"Latitude":32.696807,"paixu":22,"count":19},{"idd":58,"id":780,"sheng":"江苏","gangkou":"盐城","Longitude":120.143807,"Latitude":33.387329,"paixu":7,"count":25},{"idd":50,"id":760,"sheng":"湖南","gangkou":"岳阳","Longitude":113.120926,"Latitude":29.388954,"paixu":40,"count":32},{"idd":29,"id":731,"sheng":"安徽","gangkou":"安庆","Longitude":117.060696,"Latitude":30.545604,"paixu":31,"count":25},{"idd":65,"id":719,"sheng":"江苏","gangkou":"宿迁","Longitude":118.293796,"Latitude":33.987587,"paixu":8,"count":17},{"idd":51,"id":705,"sheng":"湖北","gangkou":"荆州","Longitude":112.237856,"Latitude":30.348842,"paixu":37,"count":39},{"idd":69,"id":704,"sheng":"安徽","gangkou":"蚌埠","Longitude":117.370702,"Latitude":32.960876,"paixu":24,"count":4},{"idd":63,"id":654,"sheng":"浙江","gangkou":"杭州","Longitude":120.157892,"Latitude":30.284994,"paixu":15,"count":10},{"idd":71,"id":635,"sheng":"安徽","gangkou":"阜阳","Longitude":115.806932,"Latitude":32.960876,"paixu":23,"count":8},{"idd":48,"id":511,"sheng":"湖北","gangkou":"咸宁","Longitude":114.298353,"Latitude":29.868959,"paixu":38,"count":28},{"idd":61,"id":469,"sheng":"浙江","gangkou":"湖州","Longitude":120.093502,"Latitude":30.913646,"paixu":16,"count":27},{"idd":57,"id":445,"sheng":"江苏","gangkou":"连云港","Longitude":119.223859,"Latitude":34.621197,"paixu":12,"count":14},{"idd":67,"id":407,"sheng":"山东","gangkou":"枣庄","Longitude":117.324709,"Latitude":34.832475,"paixu":18,"count":9},{"idd":16,"id":390,"sheng":"安徽","gangkou":"合肥","Longitude":117.223523,"Latitude":31.884168,"paixu":21,"count":29},{"idd":62,"id":377,"sheng":"浙江","gangkou":"嘉兴","Longitude":120.762991,"Latitude":30.762828,"paixu":17,"count":26},{"idd":72,"id":324,"sheng":"河南","gangkou":"周口","Longitude":114.692501,"Latitude":33.675271,"paixu":46,"count":2},{"idd":20,"id":308,"sheng":"安徽","gangkou":"滁州","Longitude":118.335137,"Latitude":32.274732,"paixu":26,"count":7},{"idd":74,"id":272,"sheng":"湖南","gangkou":"常德","Longitude":111.687554,"Latitude":29.080121,"paixu":41,"count":6},{"idd":23,"id":196,"sheng":"河南","gangkou":"信阳","Longitude":114.083788,"Latitude":32.170211,"paixu":45,"count":5},{"idd":15,"id":187,"sheng":"安徽i","gangkou":"宣城","Longitude":118.768896,"Latitude":30.969151,"paixu":27,"count":6},{"idd":56,"id":173,"sheng":"四川","gangkou":"宜宾","Longitude":104.625974,"Latitude":28.792846,"paixu":49,"count":2},{"idd":32,"id":156,"sheng":"浙江","gangkou":"舟山","Longitude":122.209478,"Latitude":30.033184,"paixu":50,"count":4},{"idd":76,"id":154,"sheng":"福建","gangkou":"福州","Longitude":119.279421,"Latitude":26.101324,"paixu":51,"count":6},{"idd":55,"id":147,"sheng":"四川","gangkou":"泸州","Longitude":105.417058,"Latitude":28.792846,"paixu":48,"count":2},{"idd":89,"id":134,"sheng":"山东","gangkou":"青岛","Longitude":120.386465,"Latitude":36.080511,"paixu":52,"count":0}];
var resArr =  [];

app.get('/', function(req, res) 
{
    requestDate(req, res);
});


function requestDate(req,res)
{
    var res = res;
    var req = req;

    var city = portArr[portIndex].gangkou;
    var gangkou = encodeURI(city);
    var url = `http://www.chuanhuobu2.com/mercury/api/search/palletTargetKeyword?q=${gangkou}`;
    var headers = {
        'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
        'Proxy-Connection':'keep-alive',
        'Cookie':'anonid=109b54dd5ca746cfa49b9b7c979e9ccb; scauth=e6ecca76-20a1-4111-8aa0-247480cacc02; SERVERID=a63d4940071124f5c8800d7e9823ca20|1504079153|1504071973',
        'User-Agent':'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.113 Safari/537.36'
    };

    console.log('startReqest:'+url + ' gangkou:'+ city);
    request({
        url: url,
        encoding: 'utf-8',
        headers: headers,
    }, function(error, response, body) {
        console.log('\nerror:'+error+'\nresponse:'+JSON.stringify(response)+'\nbody:' + body);
        if (response && response.statusCode == 200) {
            var data = JSON.parse(body);
            resArr.push({"city":city,"gangkou":data});
            portIndex++;
             console.log('\nresArr:'+JSON.stringify(resArr));

            if (portIndex < portArr.length) {
                sleep.sleep(3);
                requestDate(req,res);
            }else{
                var fs = require("fs");
                var filePath = '../resArr.json';
                fs.writeFile(filePath, JSON.stringify(resArr), function(err){
                    console.log('write file path:'+filePath+' err:'+err);
                });
                res.send({
                    msg: "success",
                    code: 1,
                    data:resArr
                });
            return;
            }
        } else {
            console.log('error:' + error +'response.statusCode:'+response.statusCode);
            portIndex = -1;
            res.send({
                msg: "糟糕!!! 网络好像有点问题",
                code: 0
            })
        }
    });
}

module.exports = app;



